#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include "caml.h"
#include "constantes.h"
#include "structures.h"
#include "interface.h"
#include "explorer.h"
#include "tools.h"
#include "debug.h"

p_context		context_add_window(p_context		c,
					   int			new,
					   value		ba)
{
  int			*p;

  p = Data_bigarray_val(ba);
  p[IBA_UPDATE_WVIDEO] = 1;

  if (WIN[new]) /* already exists */
  {
    wclear(WIN[new]);
    wrefresh(WIN[new]);
    delwin(WIN[new]);
    WIN[new] = NULL;
    c->nb_win --;
    redraw(ba);

    return (c);
  }

  getmaxyx(WMAIN, c->line, c->col);
  if (c->line < HEIGHT[new] || c->col < WIDTH[new])
  {
    mvwprintw(WMAIN, 0, 0, "not enough room to add a new window\n");
    return (c);
  }

  c->nb_win ++;
  WIN[new] = WMAIN; /* giving new an immediate value (!= NULL) */
  redraw(ba); /* will fix new before displaying it */

  if (WEXPLORER)
    explorer_list(c);

  return (c);
}


value			redraw(value				ba)
{
  int			*p;
  p_context		c;
  int			vid_line, vid_col;
  int			origin_line = 0; /* free zone origin_y point */
  int			origin_col = 0;	 /* free zone origin_x point */
  int			i;


  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];

  getmaxyx(WMAIN, vid_line, vid_col);
  p[IBA_UPDATE_WVIDEO] = 1;

  if (c->nb_win == 1)
  { /* only one win => ZONE_VIDEO */
    WVIDEO = subwin(WMAIN, vid_line, vid_col, 0, 0);

    return (Val_unit);
  }

  for (i = (MAX_WIN - 1); i > 0; i--) /* end with ZONE_VIDEO */
    if (WIN[i])
    {
      wclear(WIN[i]);
      delwin(WIN[i]);

      switch (SIDE[i])
      {
      case TOP:
      	WIDTH[i] = vid_col;
	vid_line -= HEIGHT[i];
	WIN[i] = subwin(WMAIN, HEIGHT[i], WIDTH[i], origin_line, origin_col);
	origin_line += HEIGHT[i];
	break;

      case BOTTOM:
      	WIDTH[i] = vid_col;
	vid_line -= HEIGHT[i];
	WIN[i] = subwin(WMAIN, HEIGHT[i], WIDTH[i],
			origin_line + vid_line, origin_col);
	break;

      case LEFT:
      	HEIGHT[i] = vid_line;
	WIN[i] = subwin(WMAIN, HEIGHT[i], WIDTH[i], origin_line, origin_col);
	origin_col += WIDTH[i];
	vid_col -= WIDTH[i];
	break;

      case RIGHT:
      	HEIGHT[i] = vid_line;
	vid_col -= WIDTH[i];
	WIN[i] = subwin(WMAIN, HEIGHT[i], WIDTH[i],
			origin_line, origin_col + vid_col);
	break;

      default:
	if (i == ZONE_VIDEO)
	{
	  WIN[i] = subwin(WMAIN, vid_line, vid_col, origin_line, origin_col);
	  p[IBA_VIDEO_LINE] = vid_line;
	  p[IBA_VIDEO_COL] = vid_col;
	  return (Val_unit);
	}
	break;
      }

      wclear(WIN[i]);
      if (IS_FRAME)
      {
	display_frame(WIN[i]);
	display_name(c, i);
      }
      touchwin(WMAIN);
      wrefresh(WIN[i]);
    }

  return (Val_unit);
}


/* clear screen && restaure sub-windows */
value			context_refresh(value			ba)
{
  int			*p;
  p_context		c;
  int			i;

  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];
  clear();

  for (i = MAX_WIN; i > 0; i--)
    if (WIN[i])
    {
      touchwin(WIN[i]); /* grepme usefull ? */
      if (IS_FRAME)
	display_frame(WIN[i]);
    }

  if (WEXPLORER)
    explorer_list(c);

  touchwin(WMAIN);
  refresh();

  return (Val_unit);
}


void			full_screen(value			ba)
{
  int			*p;
  p_context		c;

  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];

  clean_screen(c);

  getmaxyx(stdscr, c->line, c->col);

  delwin(WVIDEO); /* we can delwin a NULL window */
  delwin(WMAIN);

  WMAIN = newwin(c->line, c->col, 0, 0);
  WVIDEO = subwin(WMAIN, c->line, c->col, 0, 0);
  p[IBA_UPDATE_WVIDEO] = 1;
}


void			clean_screen(p_context			c)
{
  int			i;

  clear();
  refresh();

  if (WEXPLORER)
    for (i = 0; i < c->nb_display_files; i++)
      delwin(SWEXPLORER[i]);

  for (i = MAX_WIN; i > 0; i--)
  {
    delwin(WIN[i]);
    wclear(WIN[i]);
    WIN[i] = NULL;
    c->nb_win --;
  }
}


void			clear_win(p_context			c,
				  int				zone)
{
  wclear(WIN[zone]);
  wrefresh(WIN[zone]);
}


void			set_subwin_size(p_context		c,
					value			ba)
{
  int			*p;
  int			vid_line, vid_col, scr_line, scr_col;

  p = Data_bigarray_val(ba);

  getmaxyx(stdscr, p[IBA_SCREEN_LINE], p[IBA_SCREEN_COL]);
  getmaxyx(WVIDEO, p[IBA_VIDEO_LINE], p[IBA_VIDEO_COL]);

  scr_line = p[IBA_SCREEN_LINE];
  scr_col = p[IBA_SCREEN_COL];

  vid_line = p[IBA_VIDEO_LINE];
  vid_col = p[IBA_VIDEO_COL];

  LIM_TOP = (scr_line - vid_line) / 2;
  LIM_BOTTOM = vid_line + ((scr_line - vid_line) / 2);
  LIM_LEFT = (scr_col - vid_col) / 2;
  LIM_RIGHT = vid_col + ((scr_col - vid_col) / 2);

  HEIGHT_EXPLORER = p[IBA_SCREEN_LINE];
  HEIGHT_PLAYLIST = p[IBA_SCREEN_LINE];
  WIDTH_INFOBAR = p[IBA_SCREEN_COL];
  WIDTH_MENUBAR = p[IBA_SCREEN_COL];

  WIDTH_PLAYLIST = max2int(scr_col / 2, LIM_LEFT);
  WIDTH_EXPLORER = max2int(scr_col / 5, LIM_LEFT);

  HEIGHT_INFOBAR = max2int(scr_line / 8, LIM_TOP);
  HEIGHT_MENUBAR = max2int(scr_line / 8, LIM_TOP);

  WIDTH_THUMB = WIDTH_PLAYLIST - 2 * IS_FRAME;
  HEIGHT_THUMB = HEIGHT_PLAYLIST / 6;
}
